package com.jd.controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.jd.util.IdUtil;

import net.sf.json.JSONObject;

public class Text1Controller {

	public static void main(String[] args) {
		 RestTemplate rest = new RestTemplate();
	        InputStream inputStream = null;
	        OutputStream outputStream = null;
		 String xml = TextController.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx76d05c6e4734c5e7&secret=9e74614875b955423a51a3f023815508");
		 JSONObject jsonMap = JSONObject.fromObject(xml);
		 System.out.println("jsonMap=" + xml);
		 Map<String, String> map = new HashMap<String, String>();
		 Iterator<String> it = jsonMap.keys(); 
		 while(it.hasNext()) { 
		  String key = (String) it.next(); 
		  String u = jsonMap.get(key).toString();
		  map.put(key, u); 
		 }
		 String accessToken = map.get("access_token");
		 try {
	            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;
	            Map<String,Object> param = new HashMap<>();
	            param.put("scene", IdUtil.getIdByUUId());
	            param.put("page", "pages/customer/customer");
	            param.put("width", 430);
	            param.put("auto_color", false);
	            Map<String,Object> line_color = new HashMap<>();
	            line_color.put("r", 0);
	            line_color.put("g", 0);
	            line_color.put("b", 0);
	            param.put("line_color", line_color);
	            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
	            HttpEntity requestEntity = new HttpEntity(param, headers);
	            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
	            byte[] result = entity.getBody();
	            inputStream = new ByteArrayInputStream(result);

	            File file = new File("D://1.png");
	            if (!file.exists()){
	                file.createNewFile();
	            }
	            outputStream = new FileOutputStream(file);
	            int len = 0;
	            byte[] buf = new byte[1024];
	            while ((len = inputStream.read(buf, 0, 1024)) != -1) {
	                outputStream.write(buf, 0, len);
	            }
	            outputStream.flush();
	        } catch (Exception e) {
	        } finally {
	            if(inputStream != null){
	                try {
	                    inputStream.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(outputStream != null){
	                try {
	                    outputStream.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	    }
}
